package com.repair.project.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.repair.project.config.R;
import com.repair.project.entity.Student;
import com.repair.project.service.StudentService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hz
 * @since 2021-01-12
 */
@RestController
@RequestMapping("/project/student")
public class StudentController {


    @Autowired
    StudentService studentService;
    @ApiOperation("接口描述：获取学生列表")
    @GetMapping("getStuList")
    public R getStuList(@RequestParam(value = "page")int page, @RequestParam(value = "limit")int limit,
                        @RequestParam(required = false, value = "stuid")Integer stuid,
                        @RequestParam(required = false, value = "name")String name,
                        @RequestParam(required = false, value = "housenumber")String housenumber,
                        @RequestParam(required = false, value = "buildingnumber")String buildingnumber){

        PageHelper.startPage(page,limit);
        List<Student> list = studentService.getStuList(stuid,name,housenumber,buildingnumber);
        PageInfo<Student> pageInfo = new PageInfo<Student>(list);

        Map<String,Object> data=new HashMap<>();//装载分页数据
        data.put("data",pageInfo.getList());//数据
        return R.ok().data("datalist",pageInfo);
    }


    @ApiOperation("接口描述:增加学生")
    @PostMapping("addStus")
    public R addStus(@RequestBody Student students){
        return R.ok().data("data",studentService.save(students));
    }

    @ApiOperation("接口描述：删除学生")
    @GetMapping("delStu")
    public R delStu(@RequestParam(value = "stuid")int stuid){
        return R.ok().data("data",studentService.removeById(stuid));
    }

    @ApiOperation("接口描述：更新学生信息")
    @PostMapping("updStu")
    public R updStu(@RequestBody Student student){
        return R.ok().data("data",studentService.updateById(student));
    }
}

